home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-15 / phbench.zip / BENCHES.C < prev    next >
Text File  |  1993-01-04  |  3KB  |  140 lines

  1. /* benches - driver for Plum Hall benchmarks */
  2. #include <stdio.h>
  3. #include <time.h>
  4.  
  5. int benchreg(), benchsho(), benchlng();
  6. int benchmul(), benchfn(), benchdbl();
  7.  
  8. void tabulate();
  9. char *fround();
  10. main(argc, argv)
  11.        int argc;
  12.        char *argv[];
  13.        {
  14.        char result[6][10];
  15.        int i;
  16.  
  17.        if (argv[1][0] != '1')
  18.                printf("argv[1] must be   1   !\n");
  19.        if (argc < 3)
  20.                {
  21.                fprintf(stderr, "usage: benches 1 'compiler-id'\n");
  22.                exit(2);
  23.                }
  24.        tabulate(benchreg, result[0]);
  25.        tabulate(benchsho, result[1]);
  26.        tabulate(benchlng, result[2]);
  27.        tabulate(benchmul, result[3]);
  28.        tabulate(benchfn,  result[4]);
  29.        tabulate(benchdbl, result[5]);
  30.        printf("\n\n");
  31.        printf("%20.20s %9s %9s %9s %9s %9s %9s\n",
  32.                "", "register", "auto", "auto", "int", "function", "auto");
  33.        printf("%20.20s %9s %9s %9s %9s %9s %9s\n",
  34.                "", "int", "short", "long", "multiply", "call+ret", "double");
  35.        printf("%22.22s ",
  36.                argv[2]);
  37.        for (i = 0; i <= 5; ++i)
  38.                printf("%9.9s ", result[i]);
  39.        printf("\n");
  40.        exit(0);
  41.        }
  42. void tabulate(fn, s)
  43.        void (*fn)();
  44.        char *s;
  45.        {
  46.        static char arg1[20];
  47.        static char *arga[3] = { "x", &arg1[0], 0 };
  48.        double before, after, microsec;
  49.        long major, major_next;
  50.  
  51.        major_next = 1;
  52.        do  {
  53.                major = major_next;
  54.                sprintf(arg1, "%ld", major);
  55.                before = (double)clock();
  56.                (*fn)(2, arga);
  57.                after = (double)clock();
  58.                major_next *= 10;
  59.                } while (after-before < 100);
  60.        microsec = 1e3 * (after - before) / CLOCKS_PER_SEC / major;
  61.        sprintf(s, "%9s ", fround(microsec, 5, 3));
  62.        }
  63.  
  64. /* fround - round double x to precision p, n significant digits
  65.  * uses static string for result - not re-entrant
  66.  * fround is an accomodation for K+R-level printf which lacks %.*e or %g
  67.  * slow, fat version - uses sprintf
  68.  */
  69. #include <stdio.h>
  70. char *fround(x, p, n)
  71.     double x;
  72.     short p;
  73.     short n;
  74.     {
  75.     double y;
  76.     double log10();
  77.     short digs;
  78.     short nlog;
  79.     static char s[40] = {0};
  80.     char fmt[20];
  81.  
  82.     sprintf(fmt, "%%.%de", n-1);
  83.     sprintf(s, fmt, x);
  84.     sscanf(s, "%lf", &y);
  85.     if (y == 0)
  86.         nlog = 0;
  87.     else
  88.         nlog = log10(y);
  89.     if (nlog < 0)
  90.         --nlog;
  91.     digs = n - nlog - 1;
  92.     if (digs < 0)
  93.         digs = 0;
  94.     else if (digs > p)
  95.         digs = p;
  96.     sprintf(fmt, "%%.%df", digs);
  97.     sprintf(s, fmt, y);
  98.     if (digs == 0)
  99.         strcat(s, ".");
  100.     while (digs++ < p)
  101.         strcat(s, " ");
  102.     return (s);
  103.     }
  104.  
  105.  
  106.  
  107.  
  108. #define main benchreg
  109. #include "benchreg.c"
  110.  
  111. #undef main
  112. #undef STOR_CL
  113. #undef TYPE
  114. #define main benchsho
  115. #include "benchsho.c"
  116.  
  117. #undef main
  118. #undef STOR_CL
  119. #undef TYPE
  120. #define main benchlng
  121. #include "benchlng.c"
  122.  
  123. #undef main
  124. #undef STOR_CL
  125. #undef TYPE
  126. #define main benchmul
  127. #include "benchmul.c"
  128.  
  129. #undef main
  130. #undef STOR_CL
  131. #undef TYPE
  132. #define main benchfn
  133. #include "benchfn.c"
  134.  
  135. #undef main
  136. #undef STOR_CL
  137. #undef TYPE
  138. #define main benchdbl
  139. #include "benchdbl.c"
  140.